package com.lzx.applib.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzx.applib.R;

/**
 * Created by Lzx on 16/12/22 0022.
 */
public class AppDialogBuilder {

    Context context;

    CharSequence title;
    CharSequence message;
    CharSequence positiveText;
    DialogInterface.OnClickListener positiveListener;
    CharSequence negativeText;
    DialogInterface.OnClickListener negativeListener;
    CharSequence neutralText;
    DialogInterface.OnClickListener neutralListener;

    boolean cancelable = true;
    DialogInterface.OnCancelListener onCancelListener;
    DialogInterface.OnDismissListener onDismissListener;

    View contentView;

    AlertDialog dialog;

    public AppDialogBuilder(@NonNull Context context) {
        this.context = context;
    }

    public AppDialogBuilder setTitle(@StringRes int titleId) {
        return setTitle(context.getText(titleId));
    }

    public AppDialogBuilder setTitle(CharSequence title) {
        this.title = title;
        return this;
    }

    public AppDialogBuilder setMessage(@StringRes int messageId) {
        return setMessage(context.getText(messageId));
    }

    public AppDialogBuilder setMessage(CharSequence message) {
        this.message = message;
        return this;
    }

    public AppDialogBuilder setPositiveButton(
            @StringRes int textId, DialogInterface.OnClickListener listener) {
        return setPositiveButton(context.getText(textId), listener);
    }

    public AppDialogBuilder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) {
        this.positiveText = text;
        this.positiveListener = listener;
        return this;
    }

    public AppDialogBuilder setNegativeButton(
            @StringRes int textId, DialogInterface.OnClickListener listener) {
        return setNegativeButton(context.getText(textId), listener);
    }

    public AppDialogBuilder setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) {
        this.negativeText = text;
        this.negativeListener = listener;
        return this;
    }

    public AppDialogBuilder setNeutralButton(
            @StringRes int textId, DialogInterface.OnClickListener listener) {
        return setNeutralButton(context.getText(textId), listener);
    }

    public AppDialogBuilder setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) {
        this.neutralText = text;
        this.neutralListener = listener;
        return this;
    }

    public AppDialogBuilder setCancelable(boolean cancelable) {
        this.cancelable = cancelable;
        return this;
    }

    public AppDialogBuilder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
        return this;
    }

    public AppDialogBuilder setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
        return this;
    }

    public AppDialogBuilder setView(int layoutResId) {
        return setView(LayoutInflater.from(context).inflate(layoutResId, null));
    }

    public AppDialogBuilder setView(View view) {
        this.contentView = view;
        return this;
    }

    public AlertDialog create() {
//        dialog = new AlertDialog.Builder(context)
//                .setView(getDialogView())
//                .setCancelable(cancelable)
//                .setOnCancelListener(onCancelListener)
//                .setOnDismissListener(onDismissListener)
//                .create();
        dialog = new AppDialog(context);
        dialog.setView(getDialogView());
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(onCancelListener);
        dialog.setOnDismissListener(onDismissListener);
        return dialog;
    }

    public AlertDialog show() {
        AlertDialog dialog = create();
        dialog.show();
        return dialog;
    }

    public View getDialogView() {
        final View dialogView = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
        TextView titleView;
        final TextView messageView;
        LinearLayout btnLayout;
        TextView btn1, btn2, btn3;
        FrameLayout contentLayout;
        int btnWeight = 0;

        titleView = (TextView) dialogView.findViewById(R.id.dialog_title);
        messageView = (TextView) dialogView.findViewById(R.id.dialog_message);
        contentLayout = (FrameLayout) dialogView.findViewById(R.id.dialog_content);
        btnLayout = (LinearLayout) dialogView.findViewById(R.id.dialog_btn_layout);
        btn1 = (TextView) dialogView.findViewById(R.id.dialog_btn_1);
        btn2 = (TextView) dialogView.findViewById(R.id.dialog_btn_2);
        btn3 = (TextView) dialogView.findViewById(R.id.dialog_btn_3);

        if (TextUtils.isEmpty(title)) {
            titleView.setVisibility(View.GONE);
            dialogView.findViewById(R.id.dialog_title_divider).setVisibility(View.GONE);
        } else {
            titleView.setText(title);
        }

        if (TextUtils.isEmpty(message)) {
            messageView.setVisibility(View.GONE);
            dialogView.findViewById(R.id.dialog_message_divider).setVisibility(View.GONE);
            if (contentView == null) {
                contentLayout.setVisibility(View.GONE);
            }
        } else {
            messageView.setText(message);
            dialogView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (dialogView.getWidth() > 0) {
                        int line = messageView.getLineCount();
                        if (line == 1) {
                            messageView.setGravity(Gravity.CENTER);
                        } else {
                            messageView.setGravity(Gravity.LEFT);
                        }
                        dialogView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            });
            dialogView.findViewById(R.id.dialog_title_divider).setVisibility(View.GONE);
        }

        if (contentView != null) {
            contentLayout.removeAllViews();
            contentLayout.addView(contentView);
        }

        if (TextUtils.isEmpty(positiveText)) {
            btn3.setVisibility(View.GONE);
            dialogView.findViewById(R.id.dialog_btn_divider_2).setVisibility(View.GONE);
        } else {
            setButton(btn3, positiveText, positiveListener);
            btnWeight++;
        }

        if (TextUtils.isEmpty(neutralText)) {
            btn2.setVisibility(View.GONE);
            dialogView.findViewById(R.id.dialog_btn_divider_1).setVisibility(View.GONE);
        } else {
            setButton(btn2, neutralText, neutralListener);
            btnWeight++;
        }

        if (TextUtils.isEmpty(negativeText)) {
            btn1.setVisibility(View.GONE);
        } else {
            setButton(btn1, negativeText, negativeListener);
            btnWeight++;
        }

        btnLayout.setWeightSum(btnWeight);

        return dialogView;
    }

    private void setButton(TextView tv, CharSequence text, final DialogInterface.OnClickListener clickListener) {
        tv.setText(text);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onClick(dialog, 0);
                }
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }

    public static class AppDialog extends AlertDialog {

        protected AppDialog(@NonNull Context context) {
            super(context);
        }

        protected AppDialog(@NonNull Context context, @StyleRes int themeResId) {
            super(context, themeResId);
        }

        protected AppDialog(
                @NonNull Context context, boolean cancelable,
                @Nullable OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }

        @Override
        protected void onStart() {
            super.onStart();
            int width = getContext().getResources().getDimensionPixelSize(R.dimen.dialog_width);
            int height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setLayout(width, height);
            getWindow().setBackgroundDrawableResource(R.drawable.bg_dialog);
        }
    }
}
